<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Goods extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = 'delete_time';

    // 追加属性
    protected $append = [
		'status_text'
    ];
    
    const STATUS_SALE = 1;
    const STATUS_DOWN = 0;
    
    public static $statusList = [
        self::STATUS_SALE => '销售中',
	    self::STATUS_DOWN => '下架'
    ];
    
    public function getStatusTextAttr($value, $data) {
    	$value = $value ? : $data['status'];
    	$list = self::$statusList;
    	return $list[$value] ?? '';
    }

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['sort' => $row[$pk]]);
        });
    }

    
	public function category(){
    	return $this->belongsTo('goodsCategory', 'category_id', 'id', '', 'left')
		    ->setEagerlyType(0);
	}


	public function brand() {
    	return $this->belongsTo('brand', 'brand_id', 'id', '', 'left')
		    ->setEagerlyType(0);
	}



}
